package com.bds.tjj.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bds.tjj.model.YearData;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.YearSumService;
import com.bds.tjj.utils.Const;

/**
 * Created by guoyu on 2018/1/29.
 */
@Controller
public class YearDetailController  extends BaseAction {
	private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(YearDetailController.class);

	@Autowired
	YearSumService yearSumService;

	@RequestMapping(value = "/data/year/detail/index/{tt}", method = RequestMethod.GET)
	public String getMonthNum(Map<String, Object> map, @PathVariable String tt) throws IOException {

		if (StringUtils.isEmpty(tt)) {
			map.put("map", "");
		} else {
			map.put("map", tt);
		}

		return "ftl/analytics/year_detail_data";
	}

	@RequestMapping(value = "/data/year/detail/get/{tt}/{start}/{end}", method = RequestMethod.GET)
	@ResponseBody
	public Result getYearSum(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable String tt,
			@PathVariable String start, @PathVariable String end) throws IOException {
		Result ret = new Result();
		Integer start_year = 0;
		Integer end_year = 0;
		try {
			start_year = Integer.parseInt(start);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, "开始年份格式不对");
			return ret;
		}
		try {
			end_year = Integer.parseInt(end);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, "结束年份格式不对");
			return ret;
		}
		try {
			List<YearData> list = yearSumService.getYearTrend(tt, start_year, end_year);
			ret.setResult(Const.SUC, list);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, null);
			LOGGER.warn("查询get_month_trend异常：", e);
		}
		return ret;
	}

}
